package com.hrw.android.player.activity;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.TabHost;

import com.hrw.android.player.R;
import com.hrw.android.player.broadcastreceiver.UpdateUiBroadcastReceiver;
import com.hrw.android.player.utils.Constants;

public class MainActivity extends TabActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.media_activity);
		super.onCreate(savedInstanceState);
		initTopButton();
		initTabHost();
		initBottomMenu();
		registerReceiver();
	}

	private void registerReceiver() {
		IntentFilter localIntentFilter = new IntentFilter();
		localIntentFilter.addAction(Constants.UPDATE_UI_ACTION);
		BroadcastReceiver updateUiBroadcastReceiver = new UpdateUiBroadcastReceiver(
				this);
		registerReceiver(updateUiBroadcastReceiver, localIntentFilter);
	}

	private void initTabHost() {
		Intent intent = new Intent(this, MenuListActivity.class);
		TabHost tabHost = getTabHost();
		TabHost.TabSpec tabSpec = tabHost.newTabSpec(
				Constants.TAB_SPEC_TAG.MAIN_SPEC_TAG.getId()).setIndicator(
				Constants.TAB_SPEC_TAG.MAIN_SPEC_TAG.getId());
		tabSpec.setContent(intent);
		tabHost.addTab(tabSpec);
		tabHost.setCurrentTab(0);
	}

	private void initTopButton() {
		final Intent toPlayerActivity = new Intent(this, PlayerActivity.class);
		ImageButton player_button = (ImageButton) findViewById(R.id.player);
		player_button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					startActivityForResult(toPlayerActivity,
							Constants.MENU_TO_PLAYER_REQUEST_CODE);
				}
				return false;
			}

		});
	}

	private void initBottomMenu() {
		// TODO It is the first button on the bottom.
		ImageButton tab_local_media = (ImageButton) findViewById(R.id.tab_local_media);
		tab_local_media.setPressed(true);
	}

	@Override
	protected void onStart() {
		super.onStart();
		// localTabHost1.setCurrentTab(0);
	}

	@Override
	protected void onRestoreInstanceState(Bundle state) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(state);
	}

	protected void dialog() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("确定要退出吗?");
		builder.setTitle("提示");
		builder.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						finish();
						android.os.Process.killProcess(android.os.Process
								.myPid());
					}
				});
		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			if (event.getAction() == KeyEvent.ACTION_DOWN
					&& event.getRepeatCount() == 0) {
				dialog();
				return true;
			}
		}
		return super.dispatchKeyEvent(event);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_MENU) {

			super.openOptionsMenu();
		}
		return true; // 一定要做完以后返回 true，或者在弹出菜单后返回true，其他键返回super，让其他键默认
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.local_main_menu, menu);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Constants.MENU_TO_PLAYER_REQUEST_CODE) {
			// Bundle bundle = data.getExtras();
			switch (resultCode) {
			case Constants.MENU_TO_PLAYER_RESULT_CODE:
				// File file = (File) bundle.get("filePath");
				// if (null != file) {
				// /music_list.add(file.getName());
				// }
				break;
			}
		}
	}
}
